import django_filters
from django.shortcuts import render
from django_filters.rest_framework import FilterSet
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

from home.models import Hotel, Bed, Order
from home.serializers import HotelSerializer, BedSerializer, OrderSerializer


# Create your views here.
class HotelView(ListAPIView):
    queryset = Hotel.objects.all()
    serializer_class = HotelSerializer

class PageC(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    page_query_param = 'page'
class DetailBedView(FilterSet):
    start_time = django_filters.CharFilter(field_name='start_time')
    name = django_filters.CharFilter(field_name='name')
    price = django_filters.NumberFilter(field_name='price', lookup_expr='lte')
    class Meta:
        model = Bed
        fields = ['start_time', 'name', 'price']


class BedRoomApiView(ListAPIView):
    queryset = Bed.objects.all().order_by('id')
    serializer_class = BedSerializer
    pagination_class = PageC
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
    filterset_class = DetailBedView

    def get(self, request,*args, **kwargs):
        bed_id = request.query_params.get('id')
        datas = Bed.objects.filter(id=bed_id).first()
        serializer = BedSerializer(datas)
        return Response({'msg':"查询单个成功", 'data':serializer.data})


    def post(self, request):
        hotel_id = request.data.get('id')
        # 查询filterset
        datas = self.filter_queryset(Bed.objects.filter(hotel_id=hotel_id))
        #分页
        page = self.paginate_queryset(datas)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)


        serializer = BedSerializer(datas, many=True)
        return Response({'msg':"查询成功", 'data':serializer.data})


class OrderApiView(ListAPIView):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
    def post(self, request):
        bed_id = request.data.get('id')
        bed = Bed.objects.filter(id=bed_id).first()
        hotel = Hotel.objects.filter(id=bed.hotel_id).first()
        order = Order.objects.create()




        print(bed_id)

        return Response({'msg':"预定成功", 'data':bed_id})

